package com.frame.work.frame.deal.device0faa;


import com.alibaba.fastjson.JSONObject;
import com.frame.work.common.constant.Const;
import com.frame.work.common.constant.TypeEnum;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.config.SpringUtil;
import com.frame.work.entity.DeviceAssociatedInformation;
import com.frame.work.entity.TblSecurityLog;
import com.frame.work.frame.deal.base.BaseFrame;
import com.frame.work.mapper.SelectDeviceMapper;

import com.frame.work.service.TblSecurityLogService;
import com.frame.work.service.impl.JpushService;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 一氧化碳探测器
 */
@Data
public class YiYangHuaTanTanCeQi_0FAA8103 extends BaseFrame {

    private String switchState;

    public YiYangHuaTanTanCeQi_0FAA8103(String gatewayMac, String data) {

        //将基础数据进行处理
        super(gatewayMac, data);

        //处理此设备特有的数据

        String reportType = StringUtils.substring(data, 40, 42);
        String alarmState = StringUtils.substring(data, data.length() - 2, data.length());
        //String fangChaiState = StringUtils.substring(data,data.length() - 6, data.length() - 4);
        //数据库操作 begin
        String reportNum = "80";
        if (reportNum.equals(reportType)) {
            //获取mapper进行数据库操作
            SelectDeviceMapper testMapper = (SelectDeviceMapper) SpringUtil.getBean(SelectDeviceMapper.class);
            List<DeviceAssociatedInformation> list = testMapper.getDevice1(this.getDeviceMac());
            if (list.size() > 0) {
                DeviceAssociatedInformation deviceRelation = list.get(0);
                String homeName = "";
                String ls_text = "";
                if (StringUtils.isNotEmpty(deviceRelation.getHomeName())) {
                    homeName = deviceRelation.getHomeName();
                }
                String deviceName = deviceRelation.getDeviceName();
                if (deviceName == "" || deviceName == null) {
                    deviceName = "一氧化碳探测器";
                }
                String type = TypeEnum.NORMAL.code();
                String alarmStateNum1 = "01";
                String alarmStateNum2 = "02";
                if (alarmStateNum1.equals(alarmState)) {

                    ls_text = "检测到有可燃气体泄漏";
                    type = TypeEnum.WARNING.code();
                } else if (alarmStateNum2.equals(alarmState)) {
                    ls_text = "解除警报";
                    type = TypeEnum.NORMAL.code();
                }

                String content = homeName + deviceName + ls_text;

                /**
                 * 云片短信
                 */
                /**
                 * 极光推送
                 */
                String orgCode = deviceRelation.getOrganizationCode();
                String userId = deviceRelation.getUserId();
                String deviceId = deviceRelation.getDeviceId();
                String sourceId = deviceRelation.getSourceId();
                String sourceType = deviceRelation.getSourceType();

                if (StringUtils.isNotEmpty(sourceId)) {

                    JpushService jPushService = (JpushService) SpringUtil.getBean(JpushService.class);
                    try {
                        jPushService.sendNoSave("", content, type, sourceId, Const.MsgType.MSG_WARNING_TYPE, deviceId, userId, sourceType, orgCode);

                    } catch (Exception e) {

                    }


                }
                //极光推送部分（代码存在残缺）
                    /* if (result[0].source_id != null && result[0].source_id != '' && result[0].source_id != undefined) {
                            if (alarmState == '01') {
                                var extrasObj = {
                                    'type': Const.EXTRAS_CODE_ALARM_DEVICE,
                                    'content': result[0].device_id
                                };
                                JiGuang.push(result[0].source_id, '', content, content, extrasObj, result[0].account, Const.MSG_WARNING_TYPE,result[0].organization_code);
                            } else {
                                JiGuang.push(result[0].source_id, '', content, content, '', result[0].account,'',result[0].organization_code);
                            }
                        }*/


                //保存报警信息
                TblSecurityLog tblSecurityLog = new TblSecurityLog();
                tblSecurityLog.setGatewayMac(this.getGatewayMac());
                tblSecurityLog.setDeviceMac(this.getDeviceMac());
                tblSecurityLog.setContent(content);
                tblSecurityLog.setType(type);

                tblSecurityLog.setUserId(userId);
                TblSecurityLogService tblSecurityLogService = (TblSecurityLogService) SpringUtil.getBean(TblSecurityLogService.class);
                tblSecurityLogService.insert(tblSecurityLog);
            }
        }

        //数据库操作 end

        this.setSwitchState(alarmState);

        //将actions信息放入json对象中
        Map<String, Object> actions = new HashMap();
        actions.put("reportType", reportType);
        actions.put("alarmState", alarmState);
        //actions.put("fangChaiState",fangChaiState);
        this.setActions(actions);


        //处理好的json对象
        String json = JSONObject.toJSONString(this);
        //将数据发送到mqtt队列中
        JsonToMqtt.sendMQTT(gatewayMac, json);

    }

}
